package com.alongstudio.account.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alongstudio.account.commons.exception.BusinessException;
import com.alongstudio.account.constants.SystemConstant;
import com.alongstudio.account.entity.DictionaryContent;
import com.alongstudio.account.entity.DictionaryType;
import com.alongstudio.account.service.IDictionaryService;

@Service("DictionaryService")
public class DictionaryService extends BaseService implements IDictionaryService {
	private String className = DictionaryService.class.getName();

	@Override
	public Map findDictType(int start, int pageSize) throws BusinessException {
		Map result = new HashMap();
//		try {
//			String hql = "from DictionaryType c ";
//			List<DictionaryType> dataList = dao.query(hql, start, pageSize);
//			result.put(SystemConstant.QUERY_RESULT_DATA, dataList);
//			String hqlCount = "select count(c.id) " + hql;
//			List l = dao.query(hqlCount);
//			int count = Integer.parseInt(l.get(0).toString());
//			result.put(SystemConstant.QUERY_RESULT_COUNT, count);
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		return result;
	}

	@Override
	public List<DictionaryType> findAllDictType() throws BusinessException {
		List<DictionaryType> result = null;
//		try {
//			String hql = "from DictionaryType c ";
//			result = dao.query(hql);
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		return result;
	}

	@Override
	public boolean createDictType(DictionaryType dictType) throws BusinessException {
		boolean result = false;
		try {
			dao.create(dictType);
			result = true;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return result;
	}

	@Override
	public boolean updateDictType(DictionaryType dictType) throws BusinessException {
		boolean result = false;
		try {
			dao.update(dictType);
			result = true;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return result;
	}

	@Override
	public boolean deleteDictType(String id) throws BusinessException {
		boolean result = false;
		try {
			// TODO 被关联的数据也要一起删
			dao.deleteByPk(DictionaryType.class, id);
			result = true;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return result;
	}

	@Override
	public List<DictionaryContent> findDictCotent(String code) throws BusinessException {
		List<DictionaryContent> result = null;
//		try {
//			String hql = "from DictionaryContent c where c.dictType.code='" + code + "'";
//			result = dao.query(hql);
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		return result;
	}

	@Override
	public Map findDictCotent(String dictTypeId, int start, int pageSize) throws BusinessException {
		Map result = new HashMap();
//		try {
//			String hql = "from DictionaryContent c where c.dictType.id=" + dictTypeId;
//			List<DictionaryContent> list = dao.query(hql, start, pageSize);
//			result.put(SystemConstant.QUERY_RESULT_DATA, list);
//			String hqlCount = "select count(c.id) " + hql;
//			List l = dao.query(hqlCount);
//			int count = Integer.parseInt(l.get(0).toString());
//			result.put(SystemConstant.QUERY_RESULT_COUNT, count);
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		return result;
	}

	@Override
	public boolean deleteDictContent(String id) throws BusinessException {
		boolean result = false;
		try {
			// TODO 被关联的数据也要一起删
			dao.deleteByPk(DictionaryContent.class, id);
			result = true;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return result;
	}

	@Override
	public boolean createDictContent(DictionaryContent dictContent) throws BusinessException {
		boolean result = false;
//		try {
//			String hql = "select max(c.sortNum) from DictionaryContent c";
//			List list = dao.query(hql);
//			Integer sortNum = SystemConstant.SORT_NUM_INIT;
//			if(list != null && list.get(0) != null){
//				 sortNum = Integer.parseInt(list.get(0).toString());
//				 sortNum++;
//			}
//			dictContent.setSortNum(sortNum);
//			dao.create(dictContent);
//			result = true;
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		return result;
	}

	@Override
	public boolean updateDictContent(DictionaryContent dictContent) throws BusinessException {
		boolean result = false;
		try {
			dao.update(dictContent);
			result = true;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return result;
	}

}
